package com.atguigu.tingshu.payment.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.account.AccountFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.order.client.OrderFeignClient;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
@SuppressWarnings({"all"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {


    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private AccountFeignClient accountFeignClient;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 保存本地交易记录
     *
     * @param paymentType 支付类型  支付类型：1301-订单 1302-充值
     * @param orderNo     订单编号
     * @param userId      用户ID
     * @return
     */
    @Override
    public PaymentInfo savePaymentInfo(String paymentType, String orderNo, Long userId) {
        //1.根据订单编号查询本地交易记录 如果存在则返回即可
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderNo, orderNo);
        PaymentInfo paymentInfoDB = paymentInfoMapper.selectOne(queryWrapper);
        if (paymentInfoDB != null) {
            return paymentInfoDB;
        }
        //2.构建本地交易记录对象
        paymentInfoDB = new PaymentInfo();
        //2.1 基本属性赋值 用户ID、交易类型、订单编号、付款方式（微信）、交易状态：未支付(1401)
        paymentInfoDB.setUserId(userId);
        paymentInfoDB.setOrderNo(orderNo);
        paymentInfoDB.setPaymentType(paymentType);
        paymentInfoDB.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);
        paymentInfoDB.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        //2.2 封装交易内容及金额（远程调用订单服务或者账户服务获取）
        if (SystemConstant.PAYMENT_TYPE_ORDER.equals(paymentType)) {
            //远程调用订单服务获取订单金额及购买项目（VIP会员，专辑、声音）
            OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderNo).getData();
            Assert.notNull(orderInfo, "订单不存在！");
            if(!SystemConstant.ORDER_STATUS_UNPAID.equals(orderInfo.getOrderStatus())){
                throw new GuiguException(211, "订单状态错误！");
            }
            paymentInfoDB.setAmount(orderInfo.getOrderAmount());
            paymentInfoDB.setContent(orderInfo.getOrderTitle());
        } else if (SystemConstant.PAYMENT_TYPE_RECHARGE.equals(paymentType)) {
            //远程调用账户服务获取充值金额
            RechargeInfo rechargeInfo = accountFeignClient.getRechargeInfo(orderNo).getData();
            Assert.notNull(rechargeInfo, "充值记录不存在！");
            if(!SystemConstant.ORDER_STATUS_UNPAID.equals(rechargeInfo.getRechargeStatus())){
                throw new GuiguException(211, "充值订单状态错误！");
            }
            paymentInfoDB.setAmount(rechargeInfo.getRechargeAmount());
            paymentInfoDB.setContent("充值" + rechargeInfo.getRechargeAmount());
        }
        //3.保存本地交易记录且返回
        paymentInfoMapper.insert(paymentInfoDB);

        return paymentInfoDB;
    }

    /**
     * 用户付款成功后，修改本地交易记录
     * @param transaction 微信交易对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePaymentInfoSuccess(Transaction transaction) {


        //防重复处理 -- 微信回调后如果没收到商户返回的正确格式的信息或者没收到信息会重复回调发送请求
        //获取微信发送消息的唯一标识
        String transactionId = transaction.getTransactionId();

        //使用redis中的setnx只能存一次的特性  存24小时4分钟 微信一直重复发的时间
        Boolean flag = redisTemplate.opsForValue().setIfAbsent(transactionId, transactionId, 1444, TimeUnit.MINUTES);
        if (!flag){
            return;
        }

        //防止下面出现异常 订单交易状态没有更改或者其他的状态有没更改到的，再重复回调时会因为transactionId存过到redis中直接返回不会再往下走
        //对下面做处try catch处理

        try {
            //1.根据订单编号查询本地交易记录状态
            String orderNo = transaction.getOutTradeNo();
            LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PaymentInfo::getOrderNo, orderNo);
            PaymentInfo paymentInfo = paymentInfoMapper.selectOne(queryWrapper);
            if (!SystemConstant.PAYMENT_STATUS_UNPAID.equals(paymentInfo.getPaymentStatus())) {
                //如果不是未支付：返回即可
                return;
            }
            //2.修改本地交易记录
            //2.1 本地交易记录关联微信支付交易ID
            paymentInfo.setOutTradeNo(transaction.getTransactionId());
            //2.2 更新回调时间，及回调内容
            paymentInfo.setCallbackTime(new Date());
            paymentInfo.setCallbackContent(transaction.toString());
            //2.3 将本地交易支付状态：已支付
            paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);
            paymentInfoMapper.updateById(paymentInfo);

            //3. 远程调用订单服务/账户服务 完成订单/充值状态变更：已支付
            //3.1 判断支付类型：1301-订单
            if (SystemConstant.PAYMENT_TYPE_ORDER.equals(paymentInfo.getPaymentType())) {
                Result result = orderFeignClient.orderPaySuccess(orderNo);
                if (200 != result.getCode()) {
                    throw new GuiguException(500, "远程修改订单状态异常：" + orderNo);
                }
            }

            //3.2 判断支付类型：1302-充值
            if (SystemConstant.PAYMENT_TYPE_RECHARGE.equals(paymentInfo.getPaymentType())) {
                Result result = accountFeignClient.rechargePaySuccess(orderNo);
                if (200 != result.getCode()) {
                    throw new GuiguException(500, "远程修改余额异常：" + orderNo);
                }
            }
        } catch (Exception e) {
            //避免在执行过程中出现异常，导致重试的微信回调无法进入
            redisTemplate.delete(transactionId);
        }
    }
}
